#!/bin/bash
set -e

run=$1
if [ "$1" == "" ]; then
  echo Error: please set project name, e.g: sh install.sh xxx
  exit 1
fi
workdir=$HOME/vps-monitor
echo Begin install VPS Monitor: $run
echo Workspace: $workdir
if [ ! -d "$workdir" ]; then
  mkdir -p $workdir
fi
if [ ! -d "$workdir/$run" ]; then
  mkdir -p $workdir/$run
fi
#编译
go build -o $workdir/$run/$run

#生成配置文件夹 复制项目示例配置文件到实际位置
if [ ! -d "$workdir/$run/conf" ]; then
  mkdir -p  $workdir/$run/conf
fi
if [ ! -f "$workdir/$run/conf/config.production.yaml" ]; then
  cp conf/config.yaml $workdir/$run/conf/config.production.yaml
fi
#复制项目静态文件
cp -r public $workdir/$run/public

#生成systemd 文件
serviceFilePath=$workdir/$run/$run.service
sed "s|\$name|${run}|g; s|\$work|${workdir}|g" systemd/myregister.service > $serviceFilePath
cp $serviceFilePath /etc/systemd/system/$run-vm.service
systemctl enable $run-vm.service

echo Install finish! but some step continue:
echo 1. Please monidify $workdir/$run/conf/config.production.yaml
echo 2. start service: \'systemctl start $run-vm.service\'